package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.RequiredRole;
import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.AdminTypeEnum;
import com.electromagnetic.industry.software.common.enums.PublishEnum;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.pojo.req.*;
import com.electromagnetic.industry.software.manage.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/data/ed/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    @UserOperation(value = "登陆", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> login(@RequestBody UserLoginRequest loginRequest) {
        return userService.login(loginRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @PostMapping("/createUser")
    @UserOperation(value = "新增用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> createUser(@RequestBody UserRequest userRequest) {
        return userService.createUser(userRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @PutMapping("/updateUser")
    @UserOperation(value = "编辑用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> updateUser(@RequestBody UserModiRequest userModiRequest) {
        return userService.modifyUser(userModiRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @UserOperation(value = "发布用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> publishUser(@RequestBody UserPublishRequest userPublishRequest) {
        return userService.publishUser(userPublishRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @RequestMapping(value = "/getInfo", method = RequestMethod.GET)
    @UserOperation(value = "获取某一用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> getSingleUser(GetSingleUserRequest getSingleUserRequest) {
        return userService.getSingleUser(getSingleUserRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @UserOperation(value = "查询用户信息", modelName = UserOperationModuleEnum.USER)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ElectromagneticResult<?> searchUser(@RequestBody SearchUserRequest searchUserRequest) {
        return userService.searchUser(searchUserRequest);
    }

    @RequestMapping(value = "/validateWorkNumber", method = RequestMethod.POST)
    @UserOperation(value = "校验工号唯一性", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> validateWorkNum(@RequestBody UserWorkNumRequest userWorkNumRequest) {
        return userService.validateWorkNum(userWorkNumRequest);
    }

    @RequiredRole(AdminTypeEnum.SYSTEM)
    @PostMapping(value = "/deleteUser")
    @UserOperation(value = "删除用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> deleteUser(@RequestBody UserDeleteRequest userDeleteRequest) {
        return userService.deleteUser(userDeleteRequest);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @UserOperation(value = "登出", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> logout(@RequestHeader("Authorization") String token) {
        return userService.logout(token);
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @RequestMapping(value = "/bindRole/list", method = RequestMethod.POST)
    @UserOperation(value = "查询用户信息", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> getPublishedUsers(@RequestBody SearchUserRequest searchUserRequest) {
        searchUserRequest.setIsPublished(PublishEnum.PUBLISHED.getCode().toString());
        return userService.searchUser(searchUserRequest);
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @RequestMapping(value = "/bindRoles", method = RequestMethod.POST)
    @UserOperation(value = "绑定人员与角色", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> bindRoles(@RequestBody List<UserBindRoleDTO> list) {
        if (userService.bindRoles(list)) {
            return ElectromagneticResultUtil.success(true);
        } else {
            return ElectromagneticResultUtil.fail("500", "不可绑定空用户");
        }
    }

    //    @RequiredRole(AdminTypeEnum.SYSTEM)
    @PutMapping("/changePassword")
    @UserOperation(value = "修改用户密码", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> changePassword(@RequestBody ChangePasswordRequest request) {
        return userService.changePassword(request.getUserId(), request.getNewPassword());
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @GetMapping(value = "/resetPassword/{userId}")
    @UserOperation(value = "重置密码", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> resetPassword(@PathVariable("userId") String userId) {
        return ElectromagneticResultUtil.success(userService.resetPassword(userId));
    }

    @GetMapping(value = "/getAdminType")
    @UserOperation(value = "获取当前用户的管理员类型", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> getAdminType() {
        String adminType = UserThreadLocal.getAdminType();
        if (adminType == null || adminType.isEmpty()) {
            UserThreadLocal.setSuccessInfo("", "", "查询当前用户的管理员类型失败，当前用户未登录");
            return ElectromagneticResultUtil.fail("500", "当前用户未登录");
        } else {
            UserThreadLocal.setSuccessInfo("", "", "查询当前用户的管理员类型成功");
            return ElectromagneticResultUtil.success(adminType);
        }
    }

    @PostMapping("/validateOldPassword")
    @UserOperation(value = "校验原始密码", modelName = UserOperationModuleEnum.USER)
    public ElectromagneticResult<?> validateOldPassword(@RequestBody ValidateOldPasswordRequest request) {
        return userService.validateOldPassword(request.getUserId(), request.getOldPassword());
    }
}